package com.csp.app.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.csp.app.entity.Admin;
import com.csp.app.entity.Clasz;
import com.csp.app.entity.Course;
import com.csp.app.entity.Exam;
import com.csp.app.entity.ExamGroup;
import com.csp.app.entity.Score;
import com.csp.app.entity.Student;
import com.csp.app.service.AdminService;
import com.csp.app.service.ClaszService;
import com.csp.app.service.CourseService;
import com.csp.app.service.ExamGroupService;
import com.csp.app.service.ExamService;
import com.csp.app.service.OperateLogService;
import com.csp.app.service.RedisService;
import com.csp.app.service.ScoreService;
import com.csp.app.service.StudentService;
import com.csp.app.service.SystemSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class DefaultController extends BaseController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private ClaszService claszService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private ExamGroupService examGroupService;
    @Autowired
    private ExamService examService;
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private SystemSettingService systemSettingService;
    @Autowired
    private OperateLogService operateLogService;
    @Autowired
    private RedisService redisService;

    @RequestMapping("/login")
    public Admin login(Admin admin) {
        return adminService.login(admin);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @RequestMapping("/searchAllClass")
    public List<Clasz> searchAllClass() {
        return claszService.searchAll();
    }

    @RequestMapping("/selectClassList")
    public List<Clasz> selectClassList(EntityWrapper<Clasz> wrapper) {
        return claszService.selectList(wrapper);
    }

    /**
     * 添加单条记录
     *
     * @param clasz
     * @return
     */
    @RequestMapping("/addClasz")
    public boolean addClasz(Clasz clasz) {
        return claszService.add(clasz);
    }

    /**
     * 批量添加班级
     *
     * @param claszs
     * @return
     */
    @RequestMapping("/batchAddClass")
    public boolean batchAddClass(List<Clasz> claszs) {
        return claszService.batchAdd(claszs);
    }

    /**
     * 查询所有课程
     *
     * @return
     */
    @RequestMapping("/searchAllCourse")
    public List<Course> searchAllCourse() {
        return courseService.searchAll();
    }


    /**
     * 批量添加课程
     *
     * @param courses
     * @return
     */
    @RequestMapping("/batchAddCourse")
    public boolean batchAddCourse(List<Course> courses) {
        return courseService.batchAdd(courses);
    }

    /**
     * 添加学生
     *
     * @param examGroup
     * @return
     */
    @RequestMapping("/addExamGroup")
    public boolean addExamGroup(ExamGroup examGroup) {
        return examGroupService.add(examGroup);
    }

    @RequestMapping("/selectExamGroupCount")
    public Integer selectExamGroupCount(EntityWrapper<ExamGroup> wrapper) {
        return examGroupService.selectCount(wrapper);
    }

    @RequestMapping("/selectExamGroupPage")
    public Page<ExamGroup> selectExamGroupPage(Page<ExamGroup> page, EntityWrapper<ExamGroup> wrapper) {
        return examGroupService.selectPage(page, wrapper);
    }

    /**
     * 批量添加学生
     *
     * @param examGroups
     * @return
     */
    @RequestMapping("/batchAddExamGroup")
    public boolean batchAddExamGroup(List<ExamGroup> examGroups) {
        return examGroupService.batchAdd(examGroups);
    }


    /**
     * 查询所有考试组
     *
     * @return
     */
    @RequestMapping("/searchAllExamGroup")
    public List<ExamGroup> searchAllExamGroup() {
        return examGroupService.searchAll();
    }

    @RequestMapping("/searchAllExam")
    public List<Exam> searchAllExam() {
        return examService.searchAll();
    }

    @RequestMapping("/getExamFromCacheByKey")
    public Exam getExamFromCacheByKey(String key) {
        return examService.getEntityFromCacheByKey(key);
    }

    @RequestMapping("/getCourseFromCacheByKey")
    public Course getCourseFromCacheByKey(String key) {
        return courseService.getEntityFromCacheByKey(key);
    }

    @RequestMapping("/getScoreCount")
    public Integer getScoreCount(EntityWrapper<Score> wrapper) {
        return scoreService.selectCount(wrapper);
    }

    @RequestMapping("/updateScoreById")
    public boolean updateScoreById(Score score) {
        return scoreService.updateById(score);
    }

    @RequestMapping("/selectScorePage")
    public Page<Score> selectScorePage(Page<Score> page, EntityWrapper<Score> wrapper) {
        return scoreService.selectPage(page, wrapper);
    }

    @RequestMapping("/selectScoreList")
    public List<Score> selectScoreList(EntityWrapper<Score> wrapper) {
        return scoreService.selectList(wrapper);
    }

    @RequestMapping("/selecExamCount")
    public Integer selecExamCount(EntityWrapper<Exam> wrapper) {
        return examService.selectCount(wrapper);
    }

    @RequestMapping("/selectExamPage")
    public Page<Exam> selectExamPage(Page<Exam> page, EntityWrapper<Exam> wrapper) {
        return examService.selectPage(page, wrapper);
    }


    /**
     * 添加考试
     *
     * @param exam
     * @return
     */
    @RequestMapping("/addExam")
    public boolean addExam(Exam exam) {
        return examService.add(exam);
    }

    /**
     * 批量添加考试
     *
     * @param exams
     * @return
     */
    @RequestMapping("/batchAddExam")
    public boolean batchAddExam(List<Exam> exams) {
        return examService.batchAdd(exams);
    }

    /**
     * 通过考试组查询考试
     *
     * @param groupId
     * @return
     */
    @RequestMapping("/getExamsByGroupId")
    public List<Exam> getExamsByGroupId(Integer groupId) {
        return examService.getExamsByGroupId(groupId);
    }

    /**
     * 添加成绩记录
     *
     * @param score
     * @return
     */
    @RequestMapping("/addScore")
    public boolean addScore(Score score) {
        return scoreService.add(score);
    }

    /**
     * 批量录入成绩
     *
     * @param scores
     * @return
     */
    @RequestMapping("/batchAddScore")
    public boolean batchAddScore(List<Score> scores) {
        return scoreService.batchAdd(scores);
    }


    /**
     * 个人成绩单
     *
     * @param student
     * @param examGroupId
     * @param page
     * @param limit
     * @param orderField
     * @param orderType
     * @return
     */
    @RequestMapping("/getPersonScores")
    public Page getPersonScores(Student student, Integer examGroupId, Integer page, Integer limit, String orderField, String orderType) {
        return scoreService.getPersonScores(student, examGroupId, page, limit, orderField, orderType);
    }

    /**
     * 查询年级成绩总览
     *
     * @param examGroupId
     * @return
     */
    @RequestMapping("/getGradeScore")
    public JSONArray getGradeScore(Integer examGroupId) {
        return scoreService.getGradeScore(examGroupId);
    }


    /**
     * 查询班级某课程平均分
     *
     * @param examId
     * @return
     */
    @RequestMapping("/selectCourseScoreAvgByExamId")
    public List<Map> selectCourseScoreAvgByExamId(Integer examId) {
        return scoreService.selectCourseScoreAvgByExamId(examId);
    }

    /**
     * 查询班级某课程总分
     *
     * @param examId
     * @return
     */
    @RequestMapping("/selectCourseScoreTotalByExamId")
    public List<Map> selectCourseScoreTotalByExamId(Integer examId) {
        return scoreService.selectCourseScoreTotalByExamId(examId);
    }


    /**
     * 查询班级课程平均分排名
     *
     * @param examId
     * @return
     */
    @RequestMapping("/searchCourseScoreAvgOrderMap")
    public Map<String, Map> searchCourseScoreAvgOrderMap(Integer examId) {
        return scoreService.searchCourseScoreAvgOrderMap(examId);
    }

    /**
     * 查询班级课程总分
     *
     * @param examId
     * @return
     */
    @RequestMapping("/searchCourseScoreTotalMap")
    public Map searchCourseScoreTotalMap(Integer examId) {
        return scoreService.searchCourseScoreTotalMap(examId);
    }

    /**
     * 查询年级总分排名
     *
     * @param examGroupId
     * @return
     */
    @RequestMapping("/searchTotalScoreGradeOrderMap")
    public Map<Object, Integer> searchTotalScoreGradeOrderMap(Integer examGroupId) {
        return scoreService.searchTotalScoreGradeOrderMap(examGroupId);
    }

    /**
     * 查询班级总分排名
     *
     * @param examGroupId
     * @param classId
     * @return
     */
    @RequestMapping("/searchTotalScoreClassOrderMap")
    public Map<Object, Integer> searchTotalScoreClassOrderMap(Integer examGroupId, Integer classId) {
        return scoreService.searchTotalScoreClassOrderMap(examGroupId, classId);
    }

    /**
     * 查询某学生某场考试对应的分数
     *
     * @param studentId
     * @param examId
     * @return
     */
    @RequestMapping("/getScoreByStudentAndExamId")
    public Score getScoreByStudentAndExamId(Long studentId, Integer examId) {
        return scoreService.getScoreByStudentAndExamId(studentId, examId);
    }

    /**
     * 查询单科班级排名
     *
     * @param examId
     * @param classId
     * @return
     */
    @RequestMapping("/getClassScoreOrderMap")
    public Map<Object, Integer> getClassScoreOrderMap(Integer examId, Integer classId) {
        return scoreService.getClassScoreOrderMap(examId, classId);
    }

    /**
     * 查询单个年级排名
     *
     * @param examId
     * @return
     */
    @RequestMapping("/getGradeScoreOrderMap")
    public Map<Object, Integer> getGradeScoreOrderMap(Integer examId) {
        return scoreService.getGradeScoreOrderMap(examId);
    }

    /**
     * 查询班级成绩单
     *
     * @param classId
     * @param examGroupId
     * @return
     */
    @RequestMapping("/getClassScore")
    public JSONObject getClassScore(Integer classId, Integer examGroupId) {
        return scoreService.getClassScore(classId, examGroupId);
    }

    /**
     * 构建前端展示分数信息的模板
     *
     * @param examGroupId
     * @return
     */
    @RequestMapping("/getScoreShowTemplate")
    public JSONArray getScoreShowTemplate(Integer examGroupId) {
        return scoreService.getScoreShowTemplate(examGroupId);
    }

    /**
     * 分析班级或年级某课程分数段分布情况
     *
     * @param examGroupId
     * @param classId
     * @param courseId
     * @param minScore
     * @param maxScore
     * @param granularity
     * @return
     */
    @RequestMapping("/analyzeCourseScoreScale")
    public List analyzeCourseScoreScale(Integer examGroupId, Integer classId, Integer courseId, Integer minScore, Integer maxScore, Integer granularity) {
        return scoreService.analyzeCourseScoreScale(examGroupId, classId, courseId, minScore, maxScore, granularity);
    }

    @RequestMapping("/analyzeTotalScoreScale")
    List analyzeTotalScoreScale(Integer examGroupId, Integer classId, Integer minScore
            , Integer maxScore, Integer granularity) {
        return scoreService.analyzeTotalScoreScale(examGroupId, classId, minScore, maxScore, granularity);
    }

    /**
     * 添加学生
     *
     * @param student
     * @return
     */
    @RequestMapping("/addStudent")
    public boolean addStudent(Student student) {
        return studentService.add(student);
    }


    /**
     * 批量添加学生
     *
     * @param students
     * @return
     */
    @RequestMapping("/batchAddStudent")
    public boolean batchAddStudent(List<Student> students) {
        return studentService.batchAdd(students);
    }

    /**
     * 查询那些学生参加了对应的考试
     *
     * @param examGroupId
     * @return
     */
    @RequestMapping("/selectStudentsByExamGroupId")
    public List<Object> selectStudentsByExamGroupId(Integer examGroupId) {
        return studentService.selectStudentsByExamGroupId(examGroupId);
    }

    /**
     * 分页查询
     *
     * @param student
     * @param page
     * @param limit
     * @param studentName
     * @param orderFiled
     * @param orderType
     * @return
     */
    @RequestMapping("/searchStudentSelectivePage")
    public Page<Student> searchStudentSelectivePage(Student student, Integer page, Integer limit, String studentName
            , String orderFiled, String orderType) {
        return studentService.searchSelectivePage(student, page, limit, studentName, orderFiled, orderType);
    }

    @RequestMapping("/selectCourseList")
    public List<Course> selectCourseList(EntityWrapper<Course> wrapper) {
        return courseService.selectList(wrapper);
    }

    @RequestMapping("/insertCourse")
    public boolean insertCourse(Course course) {
        return courseService.insert(course);
    }
}
